<template>
  <div>
    <div class="header">
      <img src="@/assets/images/return.png" class="header_img" @click="back"/>
      <div class="title">项目作品</div>
      <div class="save" @click="saveBack">保存</div>
    </div>
    <div class="base_message">
      <div class="inputGroup">
        <div class="input_title">作品名称</div>
        <input type="text" placeholder="请输入作品名称" class="input_value" v-model="data.projectName"/>
      </div>
      <div class="inputGroup">
        <div class="input_title">作品链接</div>
        <input type="text" placeholder="请输入作品链接" class="input_value" v-model="data.projectLink"/>
      </div>
      <div class="inputGroup">
        <div class="input_title">行业</div>
        <input type="text" placeholder="请选择行业" class="input_value" v-model="data.industry"/>
      </div>
      <div class="inputGroup">
        <div class="input_title">功能</div>
        <input type="text" placeholder="请选择功能" class="input_value" v-model="data.function"/>
      </div>
    </div>
    <div class="base_message">
      <div class="inputGroup" @click="setSelect">
        <div class="input_title flex">优先展示本作品</div>
        <div class="checkbox"><img :class="['selected_img',isAdvance?'selected_show': '']"
                                   src="@/assets/images/selected.png" class="selected_img"/></div>
      </div>
    </div>
    <div class="base_message">
      <div class="work_describe">
        <div class="work_title">作品描述</div>
        <div class="work_text"><text-area classProp="work_p" placeholder="作品介绍、功能结构说明、成果等、字数不少于60字" @sendVal="setDesc"></text-area></div>
      </div>
    </div>
    <div class="base_message">
      <div class="work_describe">
        <div class="work_title">职责</div>
        <div class="work_text"><text-area classProp="work_p" placeholder="请描述您的职责" @sendVal="setDuty"></text-area></div>
      </div>
    </div>
    <div class="base_message">
      <div class="workProve">
        <div class="prove_title">作品图片</div>
        <div class="prove_picture">
          <div class="picImage" v-for="(file, index) in files" :key="index"><div class="pic_I" v-html="file"></div><div class="deleteIcon" @click="deleteFile(index)"></div></div>
          <div ref="picBox" class="pic" @click="takePhoto"><img src="@/assets/images/proveAdd.png" class="prove-add"/></div>
        </div>
        <div class="prove_explain">最多添加9张，可以是介绍图或者截图</div>
      </div>
    </div>
    <input type="file" ref="fileUpload" @change="fileSelected" style="display: none"/>
  </div>
</template>

<script>
  import TextArea from './TextArea'
  export default {
    name: "Production",
    components: {
      TextArea
    },
    data() {
      return {
        data:{
          projectName:null,
          projectLink:null,
          industry:null,
          function: null,
          infoDesc: null,
          duty:null,
        },
        files:[],
        filesImages:[],
        isAdvance: false

      }
    },
    methods: {
      deleteFile(index){
        this.files.splice(index,1)
        this.filesImages(index,1)
      },
      fileSelected(){
        var oFile = this.$refs['fileUpload'].files[0]
        var rFilter = /^(image\/bmp|image\/gif|image\/jpeg|image\/png|image\/tiff)$/i
        //如果文件不是图片提示不是图片
        if(!rFilter.test(oFile.type)){
          Toast({
            message: '文件格式必须为图片',
            position: 'middle',
            duration: 1000
          });
        }else{
          var reader = new FileReader(), img = new Image(), src = ''

          reader.readAsDataURL(oFile)
          var self = this
          reader.onload=function(e){
            img.src=e.target.result
            src=e.target.result
          }
          img.onload = function() {
            // 图片原始尺寸
            var originWidth = this.width
            var originHeight = this.height

            // 最大尺寸限制，可通过设置宽高来实现图片压缩程度
            var maxWidth = $(self.$refs['picBox']).width(), maxHeight = $(self.$refs['picBox']).height()
            // 目标尺寸
            var targetWidth = originWidth, targetHeight = originHeight
            // 图片尺寸超过220x180的限制
            if (originWidth > maxWidth || originHeight > maxHeight) {
              if (originWidth / originHeight > maxWidth / maxHeight) {
                // 更宽，按照宽度限定尺寸
                targetWidth = 100 + '%';
                targetHeight = (originHeight * maxWidth) / (originWidth * maxHeight) * 100 + '%';
              } else {
                targetHeight = 100 + "%";
                targetWidth =  (originWidth * maxHeight) / (originHeight * maxWidth) * 100 + '%';
              }
              self.filesImages.push(oFile)
              self.files.push(`<img src="${src}" style="width: ${targetWidth}; height: ${targetHeight}"/>`)
            }else{
              self.filesImages.push(oFile)
              self.files.push(`<img src="${src}" style="width: ${targetWidth}px; height: ${targetHeight}px"/>`)
            }
          }
        }
      },
      takePhoto() {
        $(this.$refs['fileUpload']).click()
      },
      back() {
        this.$router.back()
      },
      setSelect() {
        this.isAdvance = !this.isAdvance
      },
      setDesc(text){
        this.data.infoDesc = text
      },
      setDuty(text){
        this.data.duty = text
      },
      saveBack(){
        this.$store.dispatch("action_addProduction",this.data)
        this.$router.back()
      }
    },
    mounted() {
    }
  }
</script>

<style scoped>
  .header {
    box-sizing: border-box;
    position: relative;
    display: flex;
    height: 90px;
    background-color: #8ec43e;
    align-items: center;
    padding: 0 30px;
  }

  .header_img {
    width: 17px;
    height: 30px;
  }

  .title {
    text-align: center;
    flex: 1;
    font-size: 30px;
    color: #ffff;
  }

  .save {
    font-size: 28px;
    color: #ffffff;
  }

  .base_message {
    margin-bottom: 25px;
  }

  .inputGroup {
    border-bottom: 1px solid #efefef;
    box-sizing: border-box;
    padding: 36px 30px;
    background-color: #ffffff;
    display: flex;
    align-items: center;
    height: 98px;
  }

  .input_title {
    font-size: 28px;
    font-weight: 400;
    color: rgba(43, 43, 43, 1);
  }

  .input_value {
    text-align: right;
    flex: 1;
    font-size: 28px;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
  }

  .input_value::placeholder {
    font-size: 28px;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
  }

  .checkbox {
    position: relative;
    box-sizing: border-box;
    display: flex;
    width: 30px;
    height: 30px;
    border: 2px solid rgba(153, 153, 153, 1);
    border-radius: 5px;
  }

  .flex {
    flex: 1;
  }

  .selected_img {
    position: absolute;
    left: -2px;
    top: -2px;
    transition: all 0.1s linear;
    opacity: 0;
    height: 30px;
    width: 30px;
  }

  .selected_show {
    opacity: 1;
  }

  .work_describe {
    background-color: #ffffff;
    padding: 31px 35px 31px 30px;
  }

  .work_title {
    font-size: 28px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(43, 43, 43, 1);
  }

  .work_text {
    padding: 30px 0 0 1px;
  }

  .work_p {
    width: 100%;
    font-size: 24px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
    line-height: 36px;
    word-spacing: 5px;
  }

  .work_p::placeholder {
    color: rgba(153, 153, 153, 1);
  }
  .workProve{
    background-color: #ffffff;
    padding:36px 36px 29px 30px
  }
  .prove_title{
    margin-bottom: 30px;
    font-size:28px;
    font-family:PingFangSC-Regular;
    font-weight:400;
    color:rgba(43,43,43,1);
  }
  .prove_explain{
    font-size:24px;
    font-family:PingFangSC-Regular;
    font-weight:400;
    color:rgba(153,153,153,1);
    line-height:36px;
  }
  .prove_picture{
    display: flex;
    flex-flow: row wrap;
    flex-direction: row;
    justify-content: flex-start;
    align-items: stretch;
  }
  .pic{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 220px;
    height: 180px;
    background:rgba(207,207,207,1);
    margin-bottom: 23px;
  }
  .picImage{
    position: relative;
    margin-right: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 220px;
    height: 180px;
    margin-bottom: 23px;
  }
  .pic_I{
    width: 220px;
    height: 180px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .prove-add{
    width: 60px;
    height: 60px;
  }
  .deleteIcon{
    position: absolute;
    top: 0;
    right: 0;
    width: 50px;
    height: 50px;
    background: url("~@/assets/images/del_icon@2x.png") left center / 50px 50px  no-repeat;
  }
</style>
